// 登录
var express = require('express');
var router = express.Router();
var db = require('../model/index')

/*req,前端接收参数 */
router.get('/loginUser', function (req, res) {
    var sql = `select * from userData where userName = ? and userPwd = ?`
    db(sql, [req.query.user, req.query.pwd], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data
                })
            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
// addUser  注册
router.post('/addUser', function (req, res) {
    var sql = `INSERT into userdata (userName,userPwd,userIdCard,identity,userAudit) VALUES (?,?,?,0,'true')`
    db(sql, [req.body.userName, req.body.userPwd, req.body.userIdCard], function (err, data) {

        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
// changeUser  updateUser
router.get('/changeUser', function (req, res) {
    // var sql = `UPDATE userdata SET userName=?,userPwd=? WHERE userId = ?`
    var sql = `select * from userdata WHERE userId = ?`
    db(sql, [req.query.userId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
router.post('/updateUser', function (req, res) {
    var sql = `UPDATE userdata SET userName=?,userPwd=? WHERE userId = ?`
    db(sql, [req.body.userName, req.body.userPwd, req.body.userId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
module.exports = router;